package com.slfx.single.service.impl;

import com.slfx.single.dao.OrderMapper;
import com.slfx.single.pojo.Order;
import com.slfx.single.service.OrderService;
import com.slfx.single.util.IdWorker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private IdWorker idWorker;

    @Resource
    private OrderMapper orderMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized String addOrder(Order order) {
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        //根据订单id查询订单信息
        Order orderResult = orderMapper.selectByPrimaryKey(order.getId());
        if (orderResult != null) {
            return "repeat request";
        }
        int result = orderMapper.insert(order);
        if (result > 0) {
            return "success";
        }
        return "fail";
    }
}
